home *** CD-ROM | disk | FTP | other *** search
- Path: news.mel.aone.net.au!usenet
- From: clyde@hitech.com.au (Clyde Smith-Stubbs)
- Newsgroups: comp.lang.c
- Subject: Re: help about structure
- Date: Tue, 02 Jan 1996 22:09:53 GMT
- Organization: HI-TECH Software
- Message-ID: <30e9ac58.1078188864@news.bne.aone.net.au>
- References: <4c3t9q$aob@chleuasme.francenet.fr>
- Reply-To: clyde@hitech.com.au
- NNTP-Posting-Host: skyhawk.hitech.com.au
- X-Newsreader: Forte Agent .99c/16.141
-
- On 30 Dec 1995 17:35:54 GMT, jo <jobloch@micronet.fr> wrote:
-
- >How can I translate from Pascal to C such a structure:
- >token=record;
- > begin
- > nature:tokennature;
- > case nature : tokennature of
- > tkinteger: (i:integer);
- > tkreal: (r:double);
- > ...
- > end;
- >
-
- Like this:
-
- struct token
- {
- enum {tkinteger, tkreal } nature;
- union {
- int i;
- double r;
- } tk_u;
- } token;
-
- You would subsequently have code like:
-
- switch(token.nature) {
-
- case tkinteger:
- printf("Int value is %d\n", token.tk_u.i);
- break;
-
- case tkreal:
- printf("Float value is %f\n", token.tk_u.r);
- break;
- }
-
- but note that it's up to you to make sure that you maintain the
- relationship between the "nature" value and what is stored in the
- union - there is no run-time check.
-
-
- Clyde Smith-Stubbs | HI-TECH Software, | Voice: +61 7 3300 5011
- clyde@hitech.com.au | P.O. Box 103, Alderley, | Fax: +61 7 3300 5246
- http://www.hitech.com.au | QLD, 4051, AUSTRALIA. | BBS: +61 7 3300 5235
- ----------------------------------------------------------------------------
- FREE! Download our shareware (FREE for noncommercial use) MS-DOS C Compiler!
- Point your Web browser at http://www.hitech.com.au/
-